package com.github;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Collection;

public class FileEncodeTranform {
    public static void main(String[] args) {
        try {
            String srcDirPath = "E:\\尚硅谷资料\\设计模式-笔记代码课件资料\\代码\\DesignPattern";
            // 转为UTF-8编码格式源码路径
            String utf8DirPath = "E:\\尚硅谷资料\\设计模式-笔记代码课件资料\\代码\\DesignPattern-utf8";

            // 获取所有java文件
            Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[] { "java" }, true);

            for (File javaGbkFile : javaGbkFileCol) {
                System.out.println(javaGbkFile);
                // UTF8格式文件路径
                String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
                // 使用GBK读取数据，然后用UTF-8写入数据
                FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
